//
//  MCShareTicketViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCShareGoodsViewController: MCViewController {
    
    var goodsId = 0
    var gooodsImage = ""
    var goodsName = ""
    var lowestPrice = 0
    var suggestedPrice = 0

    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
        view.window?.addSubview(shareWayView)
        shareWayView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }

    
    // MARK: - Network
    
    func sendNetworking() {
        let paras = [
            "goodsId" : goodsId
        ] as [String : AnyObject]
        
        MCNetwork.POST(JAVA_getShareSmallProgramsCode, paras, vc: self, success: { (data) in
            
            let dataDict = data.dictionaryObject ?? [:]
            let urlStr = dataDict["imgUrl"] as? String ?? ""
            
            self.qrImageView.sd_setImage(with: URL.init(string: urlStr), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            
        }, failure: nil)
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "分享商品"
        self.view.backgroundColor = UIColor.white
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("分享", target: self, selector: #selector(rightItemClicked))

        
        
        goodsImageView.sd_setImage(with: URL.init(string: gooodsImage), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
        goodsNameLabel.text = goodsName
        
        moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: lowestPrice)
        
        let suggestStr = MCString_judgemantDecimalsIsZero(figure: suggestedPrice)
        
        suggestLabel.attributedText = suggestStr.MCAttributedString_deleteLine(color: UIColor.MCGray_middle)
        
        if lowestPrice >= suggestedPrice {
            suggestLabel.isHidden = true
        }
        
    }
    
    func initUI() {
        
        let bg_W = MCScreenWidth - 50
        let bg_H = bg_W * 1.5
        
        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.width.equalTo(bg_W)
            make.top.equalTo(50)
            make.height.equalTo(bg_H)
        }
        
    
        bgView.addSubview(bgImageView)
        bgImageView.snp.remakeConstraints { (make) -> Void in
            make.edges.equalTo(bgView)
        }
        
        bgImageView.addSubview(whiteView)
        whiteView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(bgImageView)
            make.height.equalTo(100)
        }
        
        
        
        bgImageView.addSubview(titleImageView)
        titleImageView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.top.equalTo(30)
            make.size.equalTo(CGSize.init(width: 150, height: 42))
        }
        
        
        let goodsImageWH = bg_W - 25 * 2
        
        
        bgImageView.addSubview(goodsImageView)
        goodsImageView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.width.height.equalTo(goodsImageWH)
            make.top.equalTo(titleImageView.snp.bottom).offset(20)
        }

        
        whiteView.addSubview(qrImageView)
        qrImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-25)
            make.centerY.equalTo(whiteView)
            make.width.height.equalTo(80)
        }
        

        whiteView.addSubview(goodsNameLabel)
        goodsNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.right.equalTo(qrImageView.snp.left).offset(-15)
            make.bottom.equalTo(qrImageView.snp.centerY).offset(-1)
        }

        
        whiteView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(goodsNameLabel)
            make.bottom.equalTo(qrImageView).offset(-10)
        }
        
        whiteView.addSubview(suggestLabel)
        suggestLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(moneyLabel.snp.right).offset(8)
            make.bottom.equalTo(moneyLabel)
        }
    }
    
    
    // MARK: - Setter & Getter
    
    lazy var bgView: MCShareBaseBackgroundView = {
        let view = MCShareBaseBackgroundView()
        return view
    }()
    
    lazy var shareWayView: MCShareBaseShareWayView = {
        let view = MCShareBaseShareWayView()
        view.superView = self.view
        view.saveView = self.bgView

        return view
    }()

    
    private lazy var titleImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Share_goodsName")
        return imageView
    }()
    
    
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Share_imageBg")
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()

    
    
    
    lazy var goodsImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 7
        view.layer.masksToBounds = true
        return view
    }()
    
    
    lazy var goodsNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.numberOfLines = 2
        label.textColor = UIColor.black
        label.textAlignment = NSTextAlignment.left
        return label
    }()
    

    
    
    
    
    private lazy var qrImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()
    
    lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = UIColor.MCMain
        return label
    }()
    
    lazy var suggestLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.textColor = UIColor.MCGray_middle
        return label
    }()

}


